/**
 * 
 */
package com.py.credit.controller;

import com.alibaba.fastjson.JSONArray;
import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.Column;
import com.py.credit.entity.SysUser;
import com.py.credit.entity.ViewInfo;
import com.py.credit.entity.common.OptionalColumn;
import com.py.credit.entity.enums.AdHocQueriesTableFieldEnum;
import com.py.credit.service.ColumnService;
import com.py.credit.service.RemoteBaseService;
import com.py.credit.service.TableService;
import com.py.credit.service.ViewInfoService;
import com.py.credit.utils.ExcelUtil;
import com.py.credit.utils.MapCache;
import com.py.framework.core.common.Page;
import com.py.framework.core.helper.HttpServletHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dd
 *
 */
@RestController
@RequestMapping("/adHocQueries")
public class AdHocQueriesController {

	private final static String FRONT_TABLE_ID = "viewListTable";

	@Autowired
	private ViewInfoService viewInfoService;

	@Autowired
	private TableService tableService;

	@Autowired
	private ColumnService columnService;

	@Autowired
	private RemoteBaseService remoteBaseService;

	/**
	 * @api {get} /adHocQueries/getTableList.json 获取分类表信息
	 * @apiName getTableList
	 * @apiVersion 0.0.1
	 * @apiGroup 即席查询
	 *
	 * @apiParam {String} rootId 表所属数据库节点id (必需)
	 *
	 * @apiSuccess {String} code 状态码:1 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {Object[]} data 结果集
	 *
	 * @apiSuccessExample {json}成功返回结果:
	 *                    {"code":0,"msg":null,"count":0,"data":[{"tName":"10000010963","tableId":"200305","NAME":"法人库/信用中心/测试用的"},{"tName":"10000011097","tableId":"200355","NAME":"法人库/信用中心/测试用表"},{"tName":"10000060873","tableId":"200249","NAME":"法人库/省工商局/欠缴电费信息"},{"tName":"10000060882","tableId":"200250","NAME":"法人库/省工商局/欠缴燃气信息"},{"tName":"10000060883","tableId":"200251","NAME":"法人库/省工商局/欠缴电信费信息"},{"tName":"10000060884","tableId":"200252","NAME":"法人库/省工商局/欠缴供暖费信息"},{"tName":"10000060885","tableId":"200256","NAME":"法人库/省工商局/欠缴社会保险费信息"}]}
	 *
	 */
	@RequestMapping("/getTableList.json")

	public RestServiceResult<List<Map<String, Object>>> getTableList(
			@RequestParam(value = "rootId", defaultValue = "", required = true) String rootId) {

		List<Map<String, Object>> tableList = tableService.getTableList(rootId);

		return new RestServiceResult<List<Map<String, Object>>>(tableList);

	}

	/**
	 * @api {get} /adHocQueries/getColumList.json 获取表列信息
	 * @apiName getColumList
	 * @apiVersion 0.0.1
	 * @apiGroup 即席查询
	 *
	 * @apiParam {String} tableId 表所属数据库节点id (必需)
	 *
	 * @apiSuccess {String} code 状态码:1 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {Object[]} data 结果集
	 *
	 * @apiSuccessExample {json} 成功返回结果
	 *                    {"code":0,"msg":null,"count":0,"data":[{"metricId":1588,"metricName":"企业名称","metricCode":"A000001","isRequire":"N","isIndex":"N","isPrimary":"N","defaultValue":null,"isTableShow":"Y","isDetailShow":"Y","showCol":null,"fieldType":"varchar","fieldLength":500,"fieldDesc":"","fieldWidget":null,"fieldDict":null,"sort":null,"isDel":null,"type":null,"aliases":null,"value":null,"metricType":null},{"metricId":1589,"metricName":"统一社会信用代码","metricCode":"A000002","isRequire":"N","isIndex":"N","isPrimary":"N","defaultValue":null,"isTableShow":"Y","isDetailShow":"Y","showCol":null,"fieldType":"varchar","fieldLength":18,"fieldDesc":"","fieldWidget":null,"fieldDict":null,"sort":null,"isDel":null,"type":null,"aliases":null,"value":null,"metricType":null}]}
	 */

	@RequestMapping("/getColumList.json")

	public RestServiceResult<List<Column>> getColumList(
			@RequestParam(value = "tableId", defaultValue = "", required = true) String tableId) {

		List<Column> columns = columnService.getColumnNodesFromComonContent(tableId);
		return new RestServiceResult<List<Column>>(columns);
	}

	/**
	 * @api {post} /adHocQueries/createView.json 创建视图
	 * @apiName createView
	 * @apiVersion 0.0.1
	 * @apiGroup 即席查询
	 *
	 * @apiParam {int} rootId 视图所属数据库id (必需)
	 * @apiParam {String} name 视图名称 (必需)
	 * @apiParam {String} descripion 视图描述 (必需)
	 * @apiParam {String} tableColumnInfo 视图组成信息 (必需)
	 * 
	 * @apiParamExample {json} 请求参数示例
	 *                  {"viewId":"1","name":"view_1","description":"视图1","tableColumnInfo":[{"tableNameEN":"qyjbxx","tableNameCN":"企业基本信息","columns":[{"metricCode":"qybm","metricName":"企业编码","metricId":1}]}]}
	 * 
	 * @apiSuccess {String} code 状态码:1 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {Object[]} data 结果集
	 * @apiSuccessExample {json} 成功返回结果
	 *                    {"code":0,"msg":null,"count":0,"data":[]}
	 * 
	 */

	@RequestMapping("/createView.json")
	public RestServiceResult<String> createView(ViewInfo viewInfo,
			@RequestParam(value = "tableColumnInfo", defaultValue = "", required = true) String tableColumnInfo,
			HttpServletRequest request) {

		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
		if (user != null && RestServiceResult.CODE_SUCCESS == user.getCode()) {
			SysUser userInfo = user.getData();
			if (null != userInfo) {
				viewInfo.setMaintainer(userInfo.getUsername());
			} else {
				ExceptionLogger.error("AdHocQueriesController#createView", "未获取到当前用户信息！");
			}
		} else {
			ExceptionLogger.error("AdHocQueriesController#createView", "未获取到当前用户信息！");
		}
		viewInfoService.addView(viewInfo, tableColumnInfo);
		return new RestServiceResult<>();
	}

	/**
	 * @api {get} /adHocQueries/viewDetail.json 视图详情
	 * @apiName viewDetail
	 * @apiVersion 0.0.1
	 * @apiGroup 即席查询
	 *
	 * @apiParam {String} rootId 视图所属数据库id 必填
	 * @apiParam {String} viewId 视图id 必填
	 * 
	 * @apiSuccess {String} code 状态码:1 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {Object} data 结果集
	 * @apiSuccessExample {json} 成功返回结果 {"code": 0,"msg": null,"count":0,"data":
	 *                    {"viewInfo": {"id": 31,"name": "日期类型","rootId":
	 *                    100000,"maintainer": "superadmin","updateTime":
	 *                    1524102868000,"description": "adfd"},"data":
	 *                    [{"tableId": null,"categoryName": null,"categoryCode":
	 *                    null,"pId": null,"tableNameCN":
	 *                    "法人库/信用中心/餐饮协会信息","tableNameEN":
	 *                    "10000070855","sharingType": null,"columns":
	 *                    [{"metricId": 1183,"metricName": "吊销日期","metricCode":
	 *                    "JG_XH_CYXH_DXRQ","isRequire": null,"isIndex":
	 *                    null,"isPrimary": null,"defaultValue":
	 *                    null,"isTableShow": null,"isDetailShow":
	 *                    null,"showCol": null,"fieldType": null,"fieldLength":
	 *                    null,"fieldDesc": null,"fieldWidget":
	 *                    null,"fieldDict": null,"sort": null,"isDel":
	 *                    null,"type": null,"aliases": null,"value":
	 *                    null,"metricType": null}]}]}}
	 * 
	 */

	@RequestMapping("/viewDetail.json")
	public RestServiceResult<Map<String, Object>> editView(
			@RequestParam(value = "viewId", defaultValue = "", required = true) String viewId,
			@RequestParam(value = "rootId", defaultValue = "", required = true) String rootId) {

		Map<String, Object> map = new HashMap<String, Object>();

		map.put("rootId", rootId);

		map.put("viewInfo", viewInfoService.getViewInfoById(viewId));

		map.put("data", viewInfoService.getDetail(viewId));

		return new RestServiceResult<Map<String, Object>>(map);
	}

	/**
	 * @api {post} /adHocQueries/updateView.json 更新视图
	 * @apiName updateView
	 * @apiVersion 0.0.1
	 * @apiGroup 即席查询
	 * 
	 * @apiParam {String} viewId 视图id 必填
	 * @apiParam {String} name 视图名称 必填
	 * @apiParam {String} description 视图描述 必填
	 * @apiParam {String} tableColumnInfo 视图组成信息 必填
	 * @apiParamExample {json} 请求参数示例
	 *                  {"viewId":"1","name":"view_entInfo","description":"基本信息视图","tableColumnInfo":[{"tableNameEN":"qyjbxx","tableNameCN":"企业基本信息","columns":[{"metricCode":"qybm","metricName":"企业编码","metricId":1}]}]}
	 * @apiSuccess {String} code 状态码:1 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccessExample {json} 成功返回结果示例
	 *                    {"code":0,"msg":null,"count":0,"data":null}
	 */

	@RequestMapping("/updateView.json")
	public RestServiceResult<String> updateView(ViewInfo viewInfo,
			@RequestParam(value = "tableColumnInfo", defaultValue = "", required = true) String tableColumnInfo) {

		viewInfoService.updateView(viewInfo, tableColumnInfo);

		return new RestServiceResult<String>();
	}

	/**
	 * @api {get} /adHocQueries/getViewList.json 获取视图列表
	 * @apiName getViewList
	 * @apiVersion 0.0.1
	 * @apiGroup 即席查询
	 *
	 * @apiParam {String} kId 视图所属数据库节点id (可选)
	 * @apiParam {String} condition 搜索条件 (可选)
	 * @apiParam {int} limit 每页展示条数 (必需)
	 * @apiParam {int} page 当前页数 (必需)
	 * @apiParamExample {json} 请求参数示例
	 *                  {"kId":"100000","limit":10,"page":1,"condition":[{"colNameEN":"name","value":"view_entInfo","type":"varchar"},{"colNameEN":"add_time","value":["2016-12-12","2017-01-01"],"type":"timestamp"},{"colNameEN":"range","value":[100,200],"type":"number"}]}
	 *
	 * @apiSuccess {String} code 状态码:1 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {Object[]} data 结果集
	 *
	 * @apiSuccessExample {json} 成功返回结果
	 *                    {"code":0,"msg":null,"count":1,"data":[{"update_time":1524102868000,"rootId":100000,"resource":"法人库","name":"日期类型","description":"基本信息视图","id":31,"maintainer":"superadmin"}]}
	 * 
	 */
	@RequestMapping(value = "/getViewList.json")
	public RestServiceResult<List<Map<String, Object>>> getTableList(
			@RequestParam(value = "kId", defaultValue = "", required = true) String kId,
			@RequestParam(value = "condition", defaultValue = "", required = true) String condition, Page page,
			HttpServletRequest request) {

		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));

		String[] configColumns = null;

		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {

			SysUser userInfo = user.getData();

			if (null != userInfo) {
				configColumns = (String[]) MapCache.get(userInfo.getId() + "_" + FRONT_TABLE_ID);
			} else {

				ExceptionLogger.error("AdHocQueriesController#getViewList", "未获取到当前用户信息！");
			}

		} else {

			ExceptionLogger.error("AdHocQueriesController#getViewList", "未获取到当前用户信息！");
		}

		List<String> showColumns = (configColumns == null || configColumns.length == 0)
				? AdHocQueriesTableFieldEnum.getDefaultShowColumnEN() : Arrays.asList(configColumns);
		String[] additionalColumns = new String[] { "id", "rootId" };
		List<Map<String, Object>> viewList = viewInfoService.getTableListForPage(kId, showColumns, additionalColumns,
				condition, page);

		return new RestServiceResult<List<Map<String, Object>>>(page.getTotalCount(), viewList);

	}

	/**
	 * @api {post} /adHocQueries/addViewListShowColunms.json 缓存视图列表展示列
	 * @apiName addViewListShowColunms
	 * @apiVersion 0.0.1
	 * @apiGroup 即席查询
	 *
	 * @apiParam {String[]} columns 列名 (必需)
	 * 
	 * @apiSuccess {String} code 状态码:1 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 *
	 * @apiSuccessExample {json} 成功返回结果
	 *                    {"code":0,"msg":null,"count":1,"data":null}
	 */

	@RequestMapping(value = "/addViewListShowColunms.json")
	public RestServiceResult<String> addViewListShowColunms(
			@RequestParam(value = "columns", required = true) String[] columns, HttpServletRequest request) {

		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));

		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {

			SysUser userInfo = user.getData();

			if (null != userInfo) {

				MapCache.set(userInfo.getId() + "_" + FRONT_TABLE_ID, columns);
			} else {

				ExceptionLogger.error("AdHocQueriesController#addViewListShowColunms", "未获取到当前用户信息！");
			}

		} else {

			ExceptionLogger.error("AdHocQueriesController#addViewListShowColunms", "未获取到当前用户信息！");
		}

		return new RestServiceResult<String>();
	}

	/**
	 * @api {get} /adHocQueries/del.json 批量删除视图
	 * @apiName del
	 * @apiVersion 0.0.1
	 * @apiGroup 即席查询
	 *
	 * @apiParam {String} ids 列id (必需)
	 * 
	 * @apiSuccess {String} code 状态码:1 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 *
	 * @apiSuccessExample {json} 成功返回结果
	 *                    {"code":0,"msg":null,"count":1,"data":null}
	 */

	@RequestMapping(value = "/del.json")
	public RestServiceResult<String> del(@RequestParam(value = "ids", defaultValue = "", required = true) String ids) {

		String[] idArray = ids.split(",");
		viewInfoService.del(idArray);
		return new RestServiceResult<String>();
	}

	/**
	 * @api {get} /adHocQueries/getViewListOptionalColums.json 获取视图列表普通搜索列和导出列
	 * @apiName getViewListOptionalColums
	 * @apiVersion 0.0.1
	 * @apiGroup 即席查询
	 *
	 * 
	 * @apiSuccess {String} code 状态码:1 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {Object[]} data 结果集
	 *
	 * @apiSuccessExample {json} 成功返回结果
	 *                    {"code":1,"msg":null,"data":[{"colNameEN":"name","colNameCN":"视图名称","type":"varchar","defaultShow":true,"widget":"input","fuzzy":true,"widgetValue":{},"searchIsShow":true},{"colNameEN":"description","colNameCN":"视图描述","type":"varchar","defaultShow":true,"widget":"input","fuzzy":false,"widgetValue":{},"searchIsShow":true},{"colNameEN":"update_time","colNameCN":"维护时间","type":"timestamp","defaultShow":true,"widget":"input","fuzzy":false,"widgetValue":{},"searchIsShow":true},{"colNameEN":"maintainer","colNameCN":"维护人","type":"varchar","defaultShow":true,"widget":"input","fuzzy":false,"widgetValue":{},"searchIsShow":true},{"colNameEN":"resource","colNameCN":"资源库","type":"varchar","defaultShow":true,"widget":"input","fuzzy":false,"widgetValue":{},"searchIsShow":true}]}
	 */

	@RequestMapping(value = "/getViewListOptionalColums.json")
	public RestServiceResult<List<OptionalColumn>> getViewListOptionalColums() {

		return new RestServiceResult<List<OptionalColumn>>(AdHocQueriesTableFieldEnum.getOptionalColumns());

	}

	/**
	 * @api {get} /adHocQueries/getViewListAdvancedConditionColumns.json
	 *      获取视图列表高级搜索列
	 * @apiName getViewListAdvancedConditionColumns
	 * @apiVersion 0.0.1
	 * @apiGroup 即席查询
	 *
	 * @apiSuccess {String} code 状态码:1 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {Object[]} data 结果集
	 *
	 * @apiSuccessExample {json} 成功返回结果
	 *                    {code:"0","msg":null,"data":[{"colNameEN":"name","colNameCN":"视图名称","type":"varchar","defaultShow":true,"widget":"input","fuzzy":true,"widgetValue":{},"searchIsShow":true},{"colNameEN":"description","colNameCN":"视图描述","type":"varchar","defaultShow":true,"widget":"input","fuzzy":false,"widgetValue":{},"searchIsShow":true},{"colNameEN":"update_time","colNameCN":"维护时间","type":"timestamp","defaultShow":true,"widget":"input","fuzzy":false,"widgetValue":{},"searchIsShow":true},{"colNameEN":"maintainer","colNameCN":"维护人","type":"varchar","defaultShow":true,"widget":"input","fuzzy":false,"widgetValue":{},"searchIsShow":true},{"colNameEN":"resource","colNameCN":"资源库","type":"varchar","defaultShow":true,"widget":"input","fuzzy":false,"widgetValue":{},"searchIsShow":true}]}
	 */

	@RequestMapping(value = "/getViewListAdvancedConditionColumns.json")
	public RestServiceResult<List<OptionalColumn>> getViewListAdvancedConditionColumns() {

		return new RestServiceResult<List<OptionalColumn>>(AdHocQueriesTableFieldEnum.getAdvancedConditionColumns());

	}

	/**
	 * @api {post} /adHocQueries/addViewResultShowColunms.json 缓存视图查询结果列表展示列
	 * @apiName addViewResultShowColunms
	 * @apiVersion 0.0.1
	 * @apiGroup 即席查询
	 * 
	 * @apiParam {String} viewId 视图id (必需)
	 * @apiParam {String[]} columns 列名 (必需)
	 * 
	 * @apiSuccess {String} code 状态码:1 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 *
	 * @apiSuccessExample {json} 成功返回结果
	 *                    {"code":0,"msg":null,"count":1,"data":null}
	 */

	@RequestMapping(value = "/addViewResultShowColunms.json")
	public RestServiceResult<String> addViewResultShowColunms(
			@RequestParam(value = "columns", required = true) String[] columns,
			@RequestParam(value = "viewId", required = true) String viewId, HttpServletRequest request) {

		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));

		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {

			SysUser userInfo = user.getData();

			if (null != userInfo) {

				MapCache.set(userInfo.getId() + "_" + viewId, columns);
			} else {

				ExceptionLogger.error("AdHocQueriesController#addViewResultShowColunms", "未获取到当前用户信息！");
			}

		} else {

			ExceptionLogger.error("AdHocQueriesController#addViewResultShowColunms", "未获取到当前用户信息！");
		}

		return new RestServiceResult<String>();
	}

	/**
	 * @api {get} /adHocQueries/getViewResultOptionalColums.json
	 *      获取视图查询结果列表普通搜索列和导出列
	 * @apiName getViewResultOptionalColums
	 * @apiVersion 0.0.1
	 * @apiGroup 即席查询
	 *
	 * @apiSuccess {String} colNameEN 列英文名
	 * @apiSuccess {String} colNameCN 列英中文名
	 * @apiSuccess {String} type 列类型
	 * @apiSuccess {String} defaultShow 是否默认显示
	 * @apiSuccess {String} widget 页面组件
	 * @apiSuccess {Boolean} fuzzy 是否支持模糊查询
	 * @apiSuccess {Boolean} searchIsShow 是否普通搜索列
	 * @apiSuccess {Object} widgetValue 列字典值
	 *
	 * @apiSuccessExample {json} 成功返回结果
	 *                    [{"colNameEN":"10000070855_JG_XH_CYXH_DXRQ","colNameCN":"吊销日期","type":"timestamp","defaultShow":true,"widget":"input","fuzzy":true,"widgetValue":null,"searchIsShow":true},{"colNameEN":"10000070855_JG_XH_CYXH_FZRQ","colNameCN":"发照日期","type":"timestamp","defaultShow":true,"widget":"input","fuzzy":true,"widgetValue":null,"searchIsShow":true}]
	 * 
	 * 
	 */

	@RequestMapping(value = "/getViewResultOptionalColums.json")
	public RestServiceResult<List<OptionalColumn>> getViewResultOptionalColums(
			@RequestParam(value = "viewId", defaultValue = "", required = true) String viewId) {

		return new RestServiceResult<List<OptionalColumn>>(viewInfoService.getViewResultOptionalColums(viewId));

	}

	/**
	 * @api {get} /adHocQueries/getViewResultAdvancedConditionColumns.json
	 *      获取视图查询结果高级检索列
	 * @apiName getViewResultAdvancedConditionColumns
	 * @apiVersion 0.0.1
	 * @apiGroup 即席查询
	 *
	 * @apiSuccess {String} colNameEN 列英文名
	 * @apiSuccess {String} colNameCN 列英中文名
	 * @apiSuccess {String} type 列类型
	 * @apiSuccess {String} defaultShow 是否默认显示
	 * @apiSuccess {String} widget 页面组件
	 * @apiSuccess {Boolean} fuzzy 是否支持模糊查询
	 * @apiSuccess {Boolean} searchIsShow 是否普通搜索列
	 * @apiSuccess {Object} widgetValue 列字典值
	 *
	 * @apiSuccessExample {json} 成功返回结果 {"code":0,"msg":null,"data":
	 *                    [{"colNameEN":"10000070855_JG_XH_CYXH_DXRQ","colNameCN":"吊销日期","type":"timestamp","defaultShow":true,"widget":"input","fuzzy":true,"widgetValue":null,"searchIsShow":true},{"colNameEN":"10000070855_JG_XH_CYXH_FZRQ","colNameCN":"发照日期","type":"timestamp","defaultShow":true,"widget":"input","fuzzy":true,"widgetValue":null,"searchIsShow":true}]}
	 * 
	 * 
	 */

	@RequestMapping(value = "/getViewResultAdvancedConditionColumns.json")
	public RestServiceResult<List<OptionalColumn>> getViewResultAdvancedConditionColumns(
			@RequestParam(value = "viewId", defaultValue = "", required = true) String viewId) {

		return new RestServiceResult<List<OptionalColumn>>(
				viewInfoService.getViewResultAdvancedConditionColumns(viewId));

	}

	/**
	 * @api {get} /adHocQueries/getViewResult.json 获取视图查询结果数据
	 * @apiName getViewResult
	 * @apiVersion 0.0.1
	 * @apiGroup 即席查询
	 * 
	 * @apiParam {String} viewId 视图id(必需)
	 * @apiParam {String} condition 搜索条件 (可选)
	 * @apiParam {int} limit 每页展示条数 (必需)
	 * @apiParam {int} page 当前页数 (必需)
	 * @apiParamExample {json} 请求参数示例
	 *                  {"viewId":"1","limit":10,"page":1,"condition":[{"colNameEN":"10000010963_F001","value":"view_entInfo","type":"varchar"},{"colNameEN":"add_time","value":["2016-12-12","2017-01-01"],"type":"timestamp"},{"colNameEN":"range","value":[100,200],"type":"number"}]}
	 *
	 * @apiSuccessExample {json} 成功返回结果
	 *                    {"code":0,"msg":null,"count":6,"data":[{"10000010963_F001":null,"10000070855_JG_XH_CYXH_DXRQ":null,"10000070855_JG_XH_CYXH_FZRQ":null,"10000010963_F0014":null},{"10000010963_F001":null,"10000070855_JG_XH_CYXH_DXRQ":null,"10000070855_JG_XH_CYXH_FZRQ":null,"10000010963_F0014":null},{"10000010963_F001":null,"10000070855_JG_XH_CYXH_DXRQ":null,"10000070855_JG_XH_CYXH_FZRQ":null,"10000010963_F0014":null},{"10000010963_F001":null,"10000070855_JG_XH_CYXH_DXRQ":null,"10000070855_JG_XH_CYXH_FZRQ":null,"10000010963_F0014":null},{"10000010963_F001":null,"10000070855_JG_XH_CYXH_DXRQ":null,"10000070855_JG_XH_CYXH_FZRQ":null,"10000010963_F0014":null},{"10000010963_F001":null,"10000070855_JG_XH_CYXH_DXRQ":null,"10000070855_JG_XH_CYXH_FZRQ":null,"10000010963_F0014":null}]}
	 * 
	 * 
	 */

	@RequestMapping(value = "/getViewResult.json")
	public RestServiceResult<List<Map<String, Object>>> getViewResult(
			@RequestParam(value = "viewId", defaultValue = "", required = true) String viewId,
			@RequestParam(value = "condition", defaultValue = "", required = true) String condition, Page page,
			HttpServletRequest request) {

		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));

		String[] configColumns = null;

		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {

			SysUser userInfo = user.getData();

			if (null != userInfo) {

				configColumns = (String[]) MapCache.get(userInfo.getId() + "_" + viewId);
			} else {

				ExceptionLogger.error("AdHocQueriesController#getViewResult", "未获取到当前用户信息！");
			}

		} else {

			ExceptionLogger.error("AdHocQueriesController#getViewResult", "未获取到当前用户信息！");
		}

		List<String> showColumns = (configColumns == null || configColumns.length == 0)
				? viewInfoService.getDefaultShowColumnEN(viewId) : Arrays.asList(configColumns);

		ViewInfo viewInfo = viewInfoService.getViewInfoById(viewId);

		List<Map<String, Object>> viewList = viewInfoService.getViewResultForPage(viewInfo.getName(), showColumns, null,
				condition, page);

		return new RestServiceResult<List<Map<String, Object>>>(page.getTotalCount(), viewList);

	}

	/**
	 * @api {get} /adHocQueries/exportViewList.json 导出视图列表
	 * @apiName exportViewList
	 * @apiVersion 0.0.1
	 * @apiGroup 即席查询
	 *
	 * @apiParam {String} kId 数据库节点id (可选)
	 * @apiParam {String} condition 搜索条件 (可选)
	 * @apiParam {String} columnEN 导出列(必需)
	 * @apiParamExample {json} 请求参数示例
	 *                  {"kId":"100000","columnEN":["name","add_time"],"condition":[{"colNameEN":"name","value":"view_entInfo","type":"varchar"},{"colNameEN":"add_time","value":["2016-12-12","2017-01-01"],"type":"timestamp"},{"colNameEN":"range","value":[100,200],"type":"number"}]}
	 * 
	 * 
	 * 
	 */

	@RequestMapping(value = "/exportViewList.json")
	public void exportViewList(@RequestParam(value = "kId", defaultValue = "", required = true) String kId,
			@RequestParam(value = "condition", defaultValue = "", required = true) String condition,
			@RequestParam(value = "columnEN", defaultValue = "", required = true) String columnEN, Page page,
			HttpServletResponse response) throws IOException {

		page.setLimit(1000);

		List<String> showColumns = JSONArray.parseArray(columnEN, String.class);
		List<Map<String, Object>> viewList = viewInfoService.getTableListForPage(kId, showColumns, null, condition,
				page);

		List<OptionalColumn> optionalColumns = AdHocQueriesTableFieldEnum.getOptionalColumns();

		ExcelUtil.downloadExcel(response, viewList, "视图列表", "视图列表", showColumns, optionalColumns);

	}

	/**
	 * @api {get} /adHocQueries/exportViewResultList.json 导出视图查询结果列表
	 * @apiName exportViewResultList
	 * @apiVersion 0.0.1
	 * @apiGroup 即席查询
	 *
	 * @apiParam {String} viewId 视图id (必需)
	 * @apiParam {String} condition 搜索条件 (可选)
	 * @apiParam {String} columnEN 导出列(必需)
	 * @apiParamExample {json} 请求参数示例
	 *                  {"viewId":"1","columnEN":["10000010963_F001","10000070855_JG_XH_CYXH_DXRQ"],"condition":[{"colNameEN":"10000010963_F001","value":"view_entInfo","type":"varchar"},{"colNameEN":"add_time","value":["2016-12-12","2017-01-01"],"type":"timestamp"},{"colNameEN":"range","value":[100,200],"type":"number"}]}
	 * 
	 * 
	 * 
	 */

	@RequestMapping(value = "/exportViewResultList.json")
	public void exportViewResultList(@RequestParam(value = "viewId", defaultValue = "", required = true) String viewId,
			@RequestParam(value = "condition", defaultValue = "", required = true) String condition,
			@RequestParam(value = "columnEN", defaultValue = "", required = true) String columnEN, Page page,
			HttpServletResponse response) throws IOException {

		page.setLimit(1000);

		List<String> showColumns = JSONArray.parseArray(columnEN, String.class);

		ViewInfo viewInfo = viewInfoService.getViewInfoById(viewId);

		List<Map<String, Object>> viewList = viewInfoService.getViewResultForPage(viewInfo.getName(), showColumns, null,
				condition, page);

		List<OptionalColumn> optionalColumns = viewInfoService.getViewResultOptionalColums(viewId);
		ExcelUtil.downloadExcel(response, viewList, "视图查询结果", "视图查询结果列表", showColumns, optionalColumns);

	}

}
